/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Produksi;

/**
 *
 * @author elfatah
 */
public class DBProduksi extends DBClass {

    public void UpdateProduksi(Produksi bb, String id) throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("UPDATE `produksi` SET `TANGGAL`=?,`JUMLAH_PRODUKSI`=?,`BIAYA`=? WHERE `PRODUKSI_ID`=?");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1, bb.getTgl());
        pstmt.setString(2, bb.getJumlahPrduksi());
        //   pstmt.setString(3, bb.getRole());
        pstmt.setString(3, bb.getBiaya());

        pstmt.setString(4, id);
        pstmt.executeUpdate();

    }

    public void deleteProduksi(String id) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("delete from produksi where `PRODUKSI_ID`=?");
        pstmt.setString(1, id);
        pstmt.executeUpdate();
    }

    public List getProduksiDetails() throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT produksi.PRODUKSI_ID, produksi.TANGGAL, barang.KODE_BARANG, produksi.JUMLAH_PRODUKSI, produksi.BIAYA FROM produksi, barang where produksi.ID_BARANG = barang.BARANG_ID");
        ResultSet rs = pstmt.executeQuery();
        List list = new ArrayList();
     
        while (rs.next()) {
   Produksi ubean = new Produksi();
            ubean.setProduksiId(rs.getString(1));
            ubean.setTgl(rs.getString(2));
            ubean.setKodeBarang(rs.getString(3));
            ubean.setJumlahPrduksi(rs.getString(4));
            ubean.setBiaya(rs.getString(5));

            list.add(ubean);

        }
        return list;
    }

    public void insertProduksi(Produksi bb) throws SQLException, ClassNotFoundException {
        try {
            Connection con = createConnection();
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO `produksi`(`TANGGAL`, `JUMLAH_PRODUKSI`, `BIAYA`) VALUES (?,?,?)");
            pstmt.setString(1, bb.getTgl());
            pstmt.setString(2, bb.getJumlahPrduksi());
            pstmt.setString(3, bb.getBiaya());

            pstmt.executeUpdate();

            PreparedStatement pstmt3 = con.prepareStatement("UPDATE `barang` SET `JUMLAH_STOK`=(`JUMLAH_STOK`-?) WHERE `BARANG_ID`=1");
            pstmt3.setString(1, bb.getJumlahPrduksi());

            pstmt3.executeUpdate();
            PreparedStatement pstmt2 = con.prepareStatement("UPDATE `barang` SET `JUMLAH_STOK`=(`JUMLAH_STOK`+?) WHERE `BARANG_ID`=2");
            pstmt2.setString(1, bb.getJumlahPrduksi());

            pstmt2.executeUpdate();

            System.out.println("testing");

        } catch (Exception e) {
            System.out.println(e);

        }
    }

    public Produksi getProduksiDetails(String id) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT PRODUKSI_ID, TANGGAL, JUMLAH_PRODUKSI, BIAYA FROM produksi, barang where produksi.ID_BARANG = barang.BARANG_ID and PRODUKSI_ID=?");
        pstmt.setString(1, id);
        ResultSet rs = pstmt.executeQuery();

        Produksi ubean = new Produksi();
        while (rs.next()) {
            ubean.setProduksiId(rs.getString(1));
            ubean.setTgl(rs.getString(2));
            ubean.setJumlahPrduksi(rs.getString(3));

            ubean.setBiaya(rs.getString(4));

        }
        ubean.setAction("Update");
        return ubean;
    }
}
